$(function(){
		var last_order = 0;
		
		$(".search_table").hide();
		$(".salary_update_cal").hide();
		
		$(".search_btn").click(function(){
			$(".search_table").hide();
			$(".search_table").fadeIn(500);
			
			var item_type = $(".search_salary_item_type option:selected").val();
			var item_name = $(".search_salary_item_name").val();
			$.ajax({
				type : "POST",
				url : "salaryItem/search",
				data:{
					"itemName":item_name,
					"itemType":item_type
				},
				success : function(data) {
					console.log(data);
					if(data.error){
						$(".alert_modal").modal('toggle');
						$(".alert_info").text(response.message);
						return;
					}
					ajax_load_table(data);
				}
			});
		});

		var ajax_load_table = function(data){	
			var html;
			var jsonData = data.response;
			$.each(jsonData,function(i){
				var salary=jsonData[i];
				html += 
					"<tr> " +
						"<td><a href='#'>"+salary.salaryItemName+"</a></td> " +
						"<td>"+salary.salaryItemType+"</td>" +
						"<td class='hidden-480'>"+salary.show+"</td>";
					if(salary.salaryCal)
						html += "<td>"+salary.salaryCal+"</td>";
					else
						html += "<td>无</td>";
					html+=
						"<td>"+salary.salaryOrder+"</td>" +
						"<td>"+salary.salaryAddItem+"</td> " +
						"<td>" +
							"<div class='hidden-sm hidden-xs btn-group'>" +
									"<button class='btn btn-xs btn-info salary_item_update_btn' item_id='"+salary.salaryItemId+"'>" +
											"<i class='ace-icon fa fa-pencil bigger-120'></i>" +
									"</button>" +
									"<button class='btn btn-xs btn-danger salary_item_delete_btn' item_id='"+salary.salaryItemId+"'>" +
											"<i class='ace-icon fa fa-trash-o bigger-120'></i>" +
									"</button>";
					if(salary.itemType=="计算项目"){
							html+=		"<button class='btn btn-xs btn-warning salary_item_update_cal_btn' item_id='"+salary.salaryItemId+"'> " +
												"<i class='ace-icon fa fa-flag bigger-120'></i>" +
										"</button>";
					}
					html+=	"</div>" +
						"</td>" +
					"</tr> ";
			});
			console.log(html);
			$(".search_container").html("");
			$(".search_container").html(html);
			
			$(".salary_item_update_btn").on("click",function() {
				$('.salary_update_modal').modal('toggle');
				$(".salary_update_type").on("change",function(){
					var item_type = $(".salary_update_type option:selected").val();
					if(item_type == "计算项目")
						$(".salary_update_cal").show();
					else
						$(".salary_update_cal").hide();
				});
			});
			
			$(".salary_item_delete_btn").on("click",function() {
				$('.salary_delete_modal').modal('toggle');
				var item_id = $(this).attr("item_id");
				$(".salary_item_delete_submit_btn").on("click",function(){
					console.log("delete : "+item_id);
					$.ajax({
						type : "POST",
						url : "salaryItem//delete",
						data:{
							"itemId":item_id
						},
						success : function(data) {
							$(".alert_modal").modal('toggle');
							$(".alert_info").text(data.message);
						}
					});
				});
			});
			
			$(".salary_item_update_cal_btn").on("click",function() {
				$('.salary_update_cal_modal').modal('toggle');
				var item_id = $(this).attr("item_id");
				console.log("update : "+item_id);
			});
		};

		$(".salary_add_cal").hide();
		$(".salary_item_add_btn").on("click",function() {
			$('.salary_add_modal').modal('toggle');
			$(".salary_add_name").val("");
			$(".salary_add_order").html("");
			$(".salary_add_memo").val("");
			
			$(".salary_add_type").on("change",function(){
				var item_type = $(".salary_add_type option:selected").val();
				if(item_type == "计算项目"){
					$(".salary_add_cal").show();
					$(".cal_item_par1").html("");
					$.ajax({
						type : "POST",
						url : "salaryItem/getCalItems",
						success : function(data) {
							console.log("calItems : "+data.response);
							$.each(data.response,function(i){
								var item = data.response[i];
								$(".cal_item_par1").append("<option value='"+item.salaryItemId+"'>"+item.salaryItemName+"</option>");
							});
						}
					});
				}
				else
					$(".salary_add_cal").hide();
			});

			$.ajax({
					type : "POST",
					url : "salaryItem/getOrder",
					success : function(data) {
						console.log("show order : "+data.response);
						last_order = data.response;
						for(var i=0;i<data.response;i++){
							var num = i+1;
							$(".salary_add_order").append("<option value='"+num+"'>第"+num+"位</option>");
						}
						var length = data.response;
						$(".salary_add_order option:nth-child("+length+")").attr("selected","selected");
					}
			});
					
			//submit add
			$(".item_add_submit_btn").one("click",function(){
				var itemName = $(".salary_add_name").val();
				var itemType = $(".salary_add_type option:selected").val();
				var itemOrder = $(".salary_add_order option:selected").val();
				var itemAdd = $(".salary_add_add option:selected").val();
				var itemShow = $(".salary_add_show option:selected").val();
				var itemMemo = $(".salary_add_memo").val();
				var itemCalItemId = $(".cal_item_par1 option:selected").val();
				var itemCalPar1 = $(".cal_item_par1 option:selected").text();
				var itemOp = $(".cal_item_op option:selected").val();
				var itemCalPar2 = $(".cal_item_par2").val();
				alert(itemCalPar1);
				console.log(itemName);
				console.log(itemType);
				console.log(itemOrder);
				console.log(itemMemo);
				console.log(itemAdd);
				console.log(itemShow);
				console.log(itemCalItemId);
				console.log(itemCalPar1);
				console.log(itemOp);
				console.log(itemCalPar2);
//				$.ajax({
//					type : "POST",
//					url : "salaryItem/add",
//					data:{
//						"itemName":itemName,
//						"itemType":itemType,
//						"lastOrder":last_order,
//						"itemOrder":itemOrder,
//						"itemAdd":itemAdd,
//						"itemShow":itemShow,
//						"itemMemo":itemMemo,
//						"itemCalItemId":itemCalItemId,
//						"itemCalPar1":itemCalPar1,
//						"itemOp":itemOp,
//						"itemCalPar2":itemCalPar2,
//					},
//					success : function(data) {
//						console.log(data);
//						$(".alert_modal").modal('toggle');
//						$(".alert_info").text(data.message);
//					}
//				});
			})
		});
		
})